// pages/jielong/confirmorder/confirmorder.js
var app = getApp(); //引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var jlpay = app.data.jlpay;
var jielong = app.data.jielong
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isblock: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      orderno: options.orderno,
      oid: options.oid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'goodsArr',
      success: function (res) {
        console.log(res)
        var totalpayPrice = 0;
        var totalgoodPrice = 0;
        var goodlist = {};
        var selectgoods = [];
        for (var i = 0; i < res.data.length; i++) {
          totalpayPrice += Number(res.data[i].oprice) * Number(res.data[i].num),
            totalgoodPrice += Number(res.data[i].gprice) * Number(res.data[i].num)
        }
        that.setData({
          goods: res.data,
          totalpayPrice: totalpayPrice.toFixed(2),
          totalgoodPrice: totalgoodPrice.toFixed(2),
          activeid: res.data[0].activeid
        })
      },
    })
  },
  pay: function () {
    var that = this;
    that.setData({
      isblock: true
    })
    wx.request({
      url: jlpay + 'op=payByWechat',
      method: 'POST',

      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        orderno: this.data.orderno,
        uniacid: parseInt(app.data.GZHid),
        openid: wx.getStorageSync('openid'),
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        activeid: parseInt(this.data.activeid),
        oid: this.data.oid
      },
      success: function (res) {
        wx.setStorage({
          key: 'goodsArr',
          data: [],
        })
        if (res.data.status == 0 || !res.data) {
          wx.redirectTo({
            url: '../retry/retry?oid=' + that.data.oid,
          })
        } else if (res.data.status == 1) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              if (res.errMsg == 'requestPayment:ok') {
                wx.request({
                  url: jielong + 'action=addJoinNum',
                  data: {
                    uniacid: app.data.GZHid,
                    id: parseInt(that.data.activeid)
                  },
                  method: "POST",
                  success: function (res) {
                    console.log(res)
                  }
                })
                wx.redirectTo({
                  url: '../paysuccess/paysuccess?totalprice=' + that.data.totalpayPrice + '&activeid=' + that.data.activeid,
                })
              }
              that.setData({
                isblock: false
              })
            },
            'fail': function (res) {
              wx.redirectTo({
                url: '../payfail/payfail',
                success: function (res) {
                  console.log(res)
                },
                fail: function (res) {
                  console.log(res)
                }
              })
              that.setData({
                isblock: false
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '../fail/fail',
          })
        }
      },
      fail: function (res) {
        that.setData({
          isblock: false
        })
      }
    })
  },
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})